﻿Imports Microsoft.VisualBasic

''' <summary>
''' 数据读取封装类
''' </summary>
''' <remarks></remarks>
Public Class DBReaders

    ''' <summary>
    ''' 初始化数据读取封装类
    ''' </summary>
    ''' <param name="db_dataTable">数据表记录集</param>
    ''' <param name="db_fieldPrefix">字段前缀(读取时可省略前缀)</param>
    ''' <remarks></remarks>
    Sub New(Optional db_dataTable As Data.DataTable = Nothing, Optional db_fieldPrefix As String = "")
        Me.dataTable = db_dataTable
        Me.fieldPrefix = db_fieldPrefix
        If db_dataTable IsNot Nothing Then
            If Me.dataTable.Columns.Contains("id") Then
                Me.dataTable.Columns.Item("id").ColumnName = Me.fieldPrefix & "id"
            End If
        End If
    End Sub

#Region "字符处理"
    ''' <summary>
    ''' 获取默认字符，如果为空则取默认值
    ''' </summary>
    ''' <param name="sObject">获取的对象</param>
    ''' <param name="defaultv">默认值</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function dValue(sObject As Object, defaultv As String) As String
        Try
            If sObject Is Nothing Then Return defaultv
            If CStr(sObject) = "" Then Return defaultv
            Return CStr(sObject)
        Catch ex As Exception
            Return defaultv
        End Try
    End Function

    ''' <summary>
    ''' 获取默认整型数字，如果为空则取默认值
    ''' </summary>
    ''' <param name="sObject">获取的对象</param>
    ''' <param name="defaultv">默认值</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function dValue(sObject As Object, defaultv As Integer) As Integer
        Try
            If sObject Is Nothing Then Return defaultv
            Return CInt(sObject)
        Catch ex As Exception
            Return defaultv
        End Try
    End Function

    ''' <summary>
    ''' 获取默认浮点数字，如果为空则取默认值
    ''' </summary>
    ''' <param name="sObject">获取的对象</param>
    ''' <param name="defaultv">默认值</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function dValue(sObject As Object, defaultv As Double) As Double
        Try
            If sObject Is Nothing Then Return defaultv
            Return CDbl(sObject)
        Catch ex As Exception
            Return defaultv
        End Try
    End Function

    ''' <summary>
    ''' 获取默认布尔值，如果为空则取默认值
    ''' </summary>
    ''' <param name="sObject">获取的对象</param>
    ''' <param name="defaultv">默认值</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function dValue(sObject As Object, defaultv As Boolean) As Boolean
        Try
            If sObject Is Nothing Then Return defaultv
            Return CBool(sObject)
        Catch ex As Exception
            Return defaultv
        End Try
    End Function

#End Region

    Dim dtR As Data.DataTable
    ''' <summary>
    ''' 数据表记录集
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property dataTable As Data.DataTable
        Get
            Return dtR
        End Get
        Set(value As Data.DataTable)
            dtR = value
        End Set
    End Property

    Dim fPrefix As String
    ''' <summary>
    ''' 字段前缀(如果设置了前缀，读取时可省略前缀)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property fieldPrefix As String
        Get
            Return Me.fPrefix
        End Get
        Set(value As String)
            Me.fPrefix = value
        End Set
    End Property

    ''' <summary>
    ''' 数据表记录行数
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property Rowscount As Integer
        Get
            If Me.dataTable Is Nothing Then
                Return 0
            Else
                Return dataTable.Rows.Count
            End If
        End Get
    End Property

    ''' <summary>
    ''' 数据表列数
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property Columncount As Integer
        Get
            If Me.dataTable Is Nothing Then
                Return 0
            Else
                Return dataTable.Columns.Count
            End If
        End Get
    End Property

    ''' <summary>
    ''' 按索引号取信息内容(返回String)
    ''' </summary>
    ''' <param name="rowIndex">行索引号</param>
    ''' <param name="colIndex">列索引号</param>
    ''' <param name="defaultValue">取值出错时默认值</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property dbItems(rowIndex As Integer, colIndex As Integer, defaultValue As String) As String
        Get
            Return dValue(dataTable.Rows(rowIndex)(colIndex).ToString, defaultValue)
        End Get
    End Property

    ''' <summary>
    ''' 按字段名取基本信息内容(返回String)
    ''' </summary>
    ''' <param name="rowIndex">行索引号</param>
    ''' <param name="colName">列名称</param>
    ''' <param name="defaultValue">取值出错时默认值</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property dbItems(rowIndex As Integer, colName As String, defaultValue As String) As String
        Get
            Return dValue(dataTable.Rows(rowIndex)(fieldPrefix & colName).ToString, defaultValue)
        End Get
    End Property

    ''' <summary>
    ''' 按索引号取信息内容(返回Integer)
    ''' </summary>
    ''' <param name="rowIndex">行索引号</param>
    ''' <param name="colIndex">列索引号</param>
    ''' <param name="defaultValue">取值出错时默认值</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property dbItems(rowIndex As Integer, colIndex As Integer, defaultValue As Integer) As Integer
        Get
            Return dValue(dataTable.Rows(rowIndex)(colIndex).ToString, defaultValue)
        End Get
    End Property

    ''' <summary>
    ''' 按字段名取基本信息内容(返回Integer)
    ''' </summary>
    ''' <param name="rowIndex">行索引号</param>
    ''' <param name="colName">列名称</param>
    ''' <param name="defaultValue">取值出错时默认值</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property dbItems(rowIndex As Integer, colName As String, defaultValue As Integer) As Integer
        Get
            Return dValue(dataTable.Rows(rowIndex)(fieldPrefix & colName).ToString, defaultValue)
        End Get
    End Property

    ''' <summary>
    ''' 按索引号取信息内容(返回Double)
    ''' </summary>
    ''' <param name="rowIndex">行索引号</param>
    ''' <param name="colIndex">列索引号</param>
    ''' <param name="defaultValue">取值出错时默认值</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property dbItems(rowIndex As Integer, colIndex As Integer, defaultValue As Double) As Double
        Get
            Return dValue(dataTable.Rows(rowIndex)(colIndex).ToString, defaultValue)
        End Get
    End Property

    ''' <summary>
    ''' 按字段名取基本信息内容(返回Integer)
    ''' </summary>
    ''' <param name="rowIndex">行索引号</param>
    ''' <param name="colName">列名称</param>
    ''' <param name="defaultValue">取值出错时默认值</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property dbItems(rowIndex As Integer, colName As String, defaultValue As Double) As Double
        Get
            Return dValue(dataTable.Rows(rowIndex)(fieldPrefix & colName).ToString, defaultValue)
        End Get
    End Property

    ''' <summary>
    ''' 按索引号取信息内容(返回Boolean)
    ''' </summary>
    ''' <param name="rowIndex">行索引号</param>
    ''' <param name="colIndex">列索引号</param>
    ''' <param name="defaultValue">取值出错时默认值</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property dbItems(rowIndex As Integer, colIndex As Integer, defaultValue As Boolean) As Boolean
        Get
            Return dValue(dataTable.Rows(rowIndex)(colIndex).ToString, defaultValue)
        End Get
    End Property

    ''' <summary>
    ''' 按字段名取基本信息内容(返回Boolean)
    ''' </summary>
    ''' <param name="rowIndex">行索引号</param>
    ''' <param name="colName">列名称</param>
    ''' <param name="defaultValue">取值出错时默认值</param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property dbItems(rowIndex As Integer, colName As String, defaultValue As Boolean) As Boolean
        Get
            Return dValue(dataTable.Rows(rowIndex)(fieldPrefix & colName).ToString, defaultValue)
        End Get
    End Property


End Class
